package com.yuyang.wifi.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuyang.wifi.R;
import com.yuyang.wifi.model.InputModel;
import com.yuyang.wifi.utils.InputEmojiFilterUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 输入框
 */

public class CustomEditText extends RelativeLayout implements TextWatcher {


    //EditText默认的文字
    private String mHintText = "";
    //EditText默认的文字 颜色
    private int mHintTextColor = Color.parseColor("#a1a1a5");
    //EditText输入的文字颜色
    private int mInputTextColor = Color.parseColor("#1f2427");
    //EditText的文字大小
    private int mInputTextSize = 13;
    //EditText内的最左边文字的左边距
    private int mPaddingLeft = 28;
    //EditText内的最上边文字的上边距
    private int mPaddingTop = 28;
    //EditText内的最右边文字的右边距
    private int mPaddingRight = 28;
    //EditText内的最下边文字的下边距
    private int mPaddingBottom = 28;


    //TextView内的文字的右边距
    private int mShowPaddingRight = 28;
    //TextView内的文字的下边距
    private int mShowPaddingBottom = 28;
    //右下角显示的文字颜色
    private int mShowTextColor = Color.parseColor("#485057");
    //右下角显示的文字大小
    private int mShowTextSize = 12;

    //输入字符串最大数量
    private int mInputCountMax = 30;
    //设置行间距
    private int mInputTextLineSpacing = 2;
    //设置背景
    private Drawable mBgDrawable;

    private View mRootView;
    private TextView mTextCount;
    private EditText mInputEdit;
    private List<InputModel> etList = new ArrayList<>();

    public CustomEditText(Context context) {
        this(context, null);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAtrrs(attrs);
        init();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
    }

    private void initAtrrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomEditText);

        mHintText = typedArray.getString(R.styleable.CustomEditText_input_text_hint);
        mHintTextColor = typedArray.getColor(R.styleable.CustomEditText_input_text_hint_color, mHintTextColor);
        mInputTextColor = typedArray.getColor(R.styleable.CustomEditText_input_text_color, mInputTextColor);
        mShowTextColor = typedArray.getColor(R.styleable.CustomEditText_show_text_color, mShowTextColor);
        mInputTextSize = typedArray.getInteger(R.styleable.CustomEditText_input_text_size, mInputTextSize);
        mShowTextSize = typedArray.getInteger(R.styleable.CustomEditText_show_text_size, mShowTextSize);
        mPaddingLeft = typedArray.getDimensionPixelSize(R.styleable.CustomEditText_padding_left, mPaddingLeft);
        mPaddingTop = typedArray.getDimensionPixelSize(R.styleable.CustomEditText_padding_top, mPaddingTop);
        mPaddingRight = typedArray.getDimensionPixelSize(R.styleable.CustomEditText_padding_right, mPaddingRight);
        mPaddingBottom = typedArray.getDimensionPixelSize(R.styleable.CustomEditText_padding_bottom, mPaddingBottom);
        mInputCountMax = typedArray.getInteger(R.styleable.CustomEditText_input_text_max, mInputCountMax);

        mShowPaddingRight = typedArray.getDimensionPixelSize(R.styleable.CustomEditText_show_padding_right, mShowPaddingRight);
        mShowPaddingBottom = typedArray.getDimensionPixelSize(R.styleable.CustomEditText_show_padding_bottom, mShowPaddingBottom);
        mInputTextLineSpacing = typedArray.getDimensionPixelSize(R.styleable.CustomEditText_input_text_line_spacing, mInputTextLineSpacing);
        mBgDrawable = typedArray.getDrawable(R.styleable.CustomEditText_bg_drawable);
        typedArray.recycle();
    }

    private void init() {

        if (mRootView == null) {
            mRootView = LayoutInflater.from(getContext()).inflate(R.layout.layout_edit_text, null);
            mInputEdit = (EditText) mRootView.findViewById(R.id.input_text);
            //mTextCount = (TextView) mRootView.findViewById(R.id.input_count);
            mInputEdit.setHintTextColor(mHintTextColor);
            mInputEdit.setTextColor(mInputTextColor);
            mInputEdit.setTextSize(mInputTextSize);
            mInputEdit.setPadding(mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom);
            mInputEdit.setHint(mHintText);
            mInputEdit.setLineSpacing(mInputTextLineSpacing, 1);
            //setFilters可以用来控制字数限制
            // mInputEdit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mInputCountMax)});
            mInputEdit.addTextChangedListener(this);
//            mTextCount.setPadding(0, 0, mShowPaddingRight, mShowPaddingBottom);
//            mTextCount.setTextColor(mShowTextColor);
//            mTextCount.setTextSize(mShowTextSize);
            //mTextCount.setText(0 + "/" + mInputCountMax);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT | RelativeLayout.ALIGN_PARENT_TOP);
            mRootView.setLayoutParams(params);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mRootView.setBackground(mBgDrawable);
            }

            etList.add(new InputModel(mInputEdit,0));
            InputEmojiFilterUtils.setEditextView(etList);
            addView(mRootView);
        }

    }

    public EditText getInputEdit() {
        return mInputEdit;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
//        int len = s.toString().trim().length();
//        if (mListener != null) {
//            mListener.observeTextLength(len);
//        }
//        if (len >= mInputCountMax) {
//            mTextCount.setText(mInputCountMax + "/" + mInputCountMax);
//            return;
//        }
//
//        mTextCount.setText(s.length() + "/" + mInputCountMax);
    }

    public void setInputTextListener(InputTextListener listener) {
        mListener = listener;
    }

    private InputTextListener mListener;

    public interface InputTextListener {

        void observeTextLength(int len);

    }
}
